package com.kyleswebspace.parser;

import java.text.NumberFormat;
import java.util.Scanner;

public class FieldParser {

	public static String parseDecimal(String in) {

		NumberFormat nf = NumberFormat.getInstance();
		
		if (in.equalsIgnoreCase("n/a"))
			return "n/a";
		
		try {
			Number num = nf.parse(in);
			return (new Double(nf.format(num.doubleValue())).toString());
		} catch(Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
	
	public static String parseInteger(String in) {

		NumberFormat nf = NumberFormat.getInstance();
		
		if (in.equalsIgnoreCase("n/a"))
			return "n/a";
		
		try {
			Number num = nf.parse(in);
			String temp = num.toString().replace(",", "");
			return temp;
		} catch(Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
	
	public static String getOnlyNumerics(String str) {
	    
	    if (str == null) {
	        return null;
	    }

	    StringBuffer strBuff = new StringBuffer();
	    char c;
	    
	    for (int i = 0; i < str.length() ; i++) {
	        c = str.charAt(i);
	        
	        if (Character.isDigit(c)) {
	            strBuff.append(c);
	        }
	    }
	    return strBuff.toString();
	}
}
